package com.hspedu.newData.dmsxl.树;

import com.hspedu.newData.dmsxl.ds.TreeNode;
import com.sun.tools.javac.Main;

/**
 * @Author: yh
 * @description: 
 * @CreateTime: 2025-05-13 
 * @Version: 1.0
 */

// 递归
public class LC563二叉树的坡度 {

    private int count = 0;

    public int findTilt(TreeNode root) {

        dfs(root);
        return count;

    }

    public int dfs(TreeNode root){

        if (root == null){
            return 0;
        }
        int sumLeft = dfs(root.left);
        int sumRight = dfs(root.right);
        count += Math.abs(sumLeft - sumRight);
        return sumLeft + sumRight + root.val;

    }
}
